import React from 'react';
import { Navigate, useLocation } from 'react-router-dom';

const checkAuth = () => {
    return localStorage.getItem('token');
}

function PrivateRoute({ children }) {
    const isAuthenticated = checkAuth(); // 实现您的身份验证逻辑
    const location = useLocation();

    if (!isAuthenticated) {
        // 重定向到登录页面，同时保存当前位置
        return <Navigate to="/login" state={{ from: location }} replace />;
    }

    return children;
}

export default PrivateRoute;
